#include "loginwidget.h"
#include "ui_loginwidget.h"

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("APlaceToStay");

    ui->passwordLineEdit->setEchoMode(QLineEdit::Password);

    //确认登陆 链接电机按钮的槽和登录函数
    connect(ui->loginButton,SIGNAL(clicked(bool)),this,SLOT(Login()));
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

void LoginWidget::on_exitButton_clicked()
{
    QApplication::exit();
}

//判断账号密码是否正确
bool LoginWidget::check(QString id,QString password)
{
    for(int i=0;i<this->infoLine.length();i++)
    {
        QString line=infoLine.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        if(id==linesplit.at(1) && password==linesplit.at(2))
        {
            return true;
        }
    }
        return false;
}

//void LoginWidget::Login()
//{
//    //获取账号密码信息
//    QString account = ui->accountLineEdit->text();
//    QString password = ui->passwordLineEdit->text();

//    //总体的容错判断
//    //账号不能为空
//    if(account.isEmpty())
//    {
//        QMessageBox msgBox;
//        msgBox.setText("Please enter the account");
//        msgBox.setWindowTitle("Error");
//        msgBox.exec();
//        return ;
//    }

//    if(password.isEmpty())//密码不能为空
//    {
//        QMessageBox msgBox;
//        msgBox.setText("Please enter the password");
//        msgBox.setWindowTitle("Error");
//        msgBox.exec();
//        return ;
//    }
//    //判断用户名密码是否正确
//    //登录学生界面
//    if(ui->studentButton->isChecked())
//    {
//        ui->accountLineEdit->clear();
//        ui->passwordLineEdit->clear();
//        bool flag = 0;
//        //判断登陆信息
//        for (int i=0;i<1;i++)//for (int i=0;i<g_Customerlist.size();i++)
//        {
//            if(1>0)//(account == g_Customerlist[i].GetAccount())&&
//                   // (password == g_Customerlist[i].GetPassword()))
//            {
//                flag = 1;
//                QMessageBox::information(this, "SUCCESS","Login success", QMessageBox::Ok);
//                this->close();
//             //   pMainwindow = new MainWindow(this, &g_Customerlist[i]);
//             //   pMainwindow->show();
//            }

//        }
//        if(flag == 0)
//        {
//            QMessageBox::information(this, "FAILED", "Account or password error",QMessageBox::Ok);
//            ui->accountLineEdit->clear();
//            ui->passwordLineEdit->clear();
//        }
//    }

//    //登录教师界面
//    else if(ui->teacherButton->isChecked())
//    {
//        ui->accountLineEdit->clear();
//        ui->passwordLineEdit->clear();
//        bool flag = 0;
//        //判断登陆信息
//        for (int i=0;i<1;i++)//for (int i=0;i<g_Customerlist.size();i++)
//        {
//            if(1>0)//(account == g_Customerlist[i].GetAccount())&&
//                   // (password == g_Customerlist[i].GetPassword()))
//            {
//                flag = 1;
//                QMessageBox::information(this, "SUCCESS","Login success", QMessageBox::Ok);
//                this->close();
//             //   pMainwindow = new MainWindow(this, &g_Customerlist[i]);
//             //   pMainwindow->show();
//            }

//        }
//        if(flag == 0)
//        {
//            QMessageBox::information(this, "FAILED", "Account or password error",QMessageBox::Ok);
//            ui->accountLineEdit->clear();
//            ui->passwordLineEdit->clear();
//        }
//    }
//    //登录管理员界面
//    else if(ui->managerButton->isChecked())
//    {
//        ui->accountLineEdit->clear();
//        ui->passwordLineEdit->clear();
//        bool flag = 0;
//        //判断登陆信息
//        for (int i=0;i<1;i++)//for (int i=0;i<g_Customerlist.size();i++)
//        {
//            if(1>0)//(account == g_Customerlist[i].GetAccount())&&
//                   // (password == g_Customerlist[i].GetPassword()))
//            {
//                flag = 1;
//                QMessageBox::information(this, "SUCCESS","Login success", QMessageBox::Ok);
//                this->close();
//             //   pMainwindow = new MainWindow(this, &g_Customerlist[i]);
//             //   pMainwindow->show();
//            }

//        }
//        if(flag == 0)
//        {
//            QMessageBox::information(this, "FAILED", "Account or password error",QMessageBox::Ok);
//            ui->accountLineEdit->clear();
//            ui->passwordLineEdit->clear();
//        }
//    }
//}

//登录按钮
void LoginWidget::on_loginButton_clicked()
{
    QString id=this->ui->idLineEdit->text();
    QString password=this->ui->passwordLineEdit->text();
    //如果选择了学生端
    if(this->ui->studentButton->isChecked())
    {
        //D:\\EducationalAdministrationSystem_Qt\\student.csv
        //C:\\Users\\10480\\Desktop\\C++BigAssignment\\12.29\\EducationalAdministrationSystem_Qt
        this->file1->readFile("student.csv",infoLine);
        for(int i=0;i<this->infoLine.length();i++)
        {
            QString line=infoLine.at(i);
            line=line.trimmed();
            QStringList linesplit=line.split(",");
        }
        if(this->check(id,password))
        {
            this->studentwidget = new StudentWidget;
            //this->studentwidget->getLoginId(id);
            this->studentwidget->show();
            //this->studentwidget->setStuObject();
            this->close();
        }
        else
        {
            QMessageBox::critical(this,"错误！","用户名或密码错误！","确定");
        }
    }
    else if(this->ui->teacherButton->isChecked())
    {
        this->file1->readFile("teacherInfo.csv",infoLine);
        if(this->check(id,password))
        {
            this->teacherwidget = new TeacherWidget;
            //this->teacherwidget->getLoginId(id);
            this->teacherwidget->show();
            //this->teacherwidget->setStuObject();
            this->close();
        }
        else
        {
            QMessageBox::critical(this,"错误！","用户名或密码错误！","确定");
        }
    }
    else if(this->ui->managerButton->isChecked())
    {
        this->file1->readFile("adminInfo.csv",infoLine);
        if(this->check(id,password))
        {
            this->managerwidget = new ManagerWidget;
            //this->managerwidget->getLoginId(id);
            this->managerwidget->show();
            //this->managerwidget->setStuObject();
            this->close();
        }
        else
        {
            QMessageBox::critical(this,"错误！","用户名或密码错误！","确定");
        }
    }
    else
    {
        QMessageBox::critical(this,"错误！","请选择类别！","确定");
    }
}
